<?php
namespace app\admin\controller;
use app\admin\model\Count as CountModel;
use app\admin\controller\Base;


class Count extends Base
{

    public function index()
    {
        $note = db('note')->count();
        if($note == 0){
            $note=1;
        }
        $note2 = $note*20;
        $user = db('user')->count();
        if($user == 0){
            $user=1;
        }
        $user2 = $user*10;
        $talk = db('user_talk')->count();
        if($talk == 0){
            $talk=1;
        }
        $talk2 = $talk*40;
        $pub = db('pub')->count();
        if($pub == 0){
            $pub=1;
        }
        $pub2 = $pub*4;
        date_default_timezone_set("PRC");
        $showapi_appid = '32428';  //替换此值,在官网的"我的应用"中找到相关值
        $showapi_secret = '1e06dfde5f1247ed910b88697f994206';  //替换此值,在官网的"我的应用"中找到相关值
        $paramArr = array(
             'showapi_appid'=> $showapi_appid,
             'cateGoryData'=> "['用户','帖子','评论','公告']",
             'title'=> "综合信息",
             'descript'=> "综合信息实时更新",
             'backgroundColor'=> "125,125,125,0",
             'yAxisFormatter'=> "",
             'width'=> "",
             'height'=> "400",
             'showToolbox'=> "",
             'showLegend'=> "",
             'smooth'=> "",
             'stack'=> "",
             'showArea'=> "",
             'chartType'=> "",
             'data'=> "[{name:'综合统计',data:[$user,$note,$talk,$pub]},{name:'季度统计',data:[$user2,$note2,$talk2,$pub2]}]"
             //添加其他参数
        );
         
        //创建参数(包括签名的处理)
        function createParam ($paramArr,$showapi_secret) {
             $paraStr = "";
             $signStr = "";
             ksort($paramArr);
             foreach ($paramArr as $key => $val) {
                 if ($key != '' && $val != '') {
                     $signStr .= $key.$val;
                     $paraStr .= $key.'='.urlencode($val).'&';
                 }
             }
             $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
             $sign = strtolower(md5($signStr));
             $paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验
            // echo "排好序的参数:".$signStr."<br>\r\n";
             return $paraStr;
        }
         
        $param = createParam($paramArr,$showapi_secret);
        $url = 'http://route.showapi.com/322-2?'.$param; 
        //echo "请求的url:".$url."<br>\r\n";
        $result = file_get_contents($url);
        /*echo "返回的json数据:<br>\r\n";
        print $result.'<br>\r\n'*/;
        $result = json_decode($result);
        /*echo "<br>\r\n取出showapi_res_code的值:<br>\r\n";
        print_r($result->showapi_res_code);
        echo "<br>\r\n"*/;
        /*echo "<pre>";
        print_r($result);
        echo "<pre>";
        die;*/
        $this->assign('countpic',$result->showapi_res_body);
        $this->assign('title','统计管理页面');
        return $this->fetch();

        
    }


}
